  # -*- coding: utf-8 -*-

import Acquisition
reload( Acquisition)
from Acquisition import *

import AE
reload( AE)
from AE import *

import Relation
reload( Relation)
from Relation import *

import RelationType
reload( RelationType)
from RelationType import *

import Attribute
reload( Attribute)
from Attribute import *

import AttributeType
reload( AttributeType)
from AttributeType import *

import Instance
reload( Instance)
from Instance import *

import InstanceAttribute
reload( InstanceAttribute)
from InstanceAttribute import *

import db
reload( db)
from db import McfConnector


class Acquisitions():
	"""Description of the patient-specific part"""
	def __init__( self, libmyCF):
		self.all_acquisitions={}
		self.all_instances={}
		self.all_instances_attributes={}
		self.libmyCF = libmyCF
		self.connector = libmyCF.connector
		self.get_all_acquisitions()
		self.get_all_instances()
		self.get_all_instances_attributes()


	def addAcquisition( self, acquisition):
		"""Add an acquisition in the database"""
		query="INSERT INTO Acquisitions ( acquisition_id, AE_id, type, date, team, comment) VALUES (NULL,'"+ str(acquisition.ae_id) +"','"+ str(acquisition.type) +"','"+ str(acquisition.date) +"','"+ str(acquisition.team) +"','"+ str(acquisition.comment) +"');"
		#print query
		self.connector.mcfQueryExec(query)
		
		# Get the new ID
		query="SELECT acquisition_id FROM Acquisitions where AE_id=\""+ str(acquisition.ae_id) +"\" and type=\""+ str(acquisition.type) +"\" and date=\""+ str(acquisition.date) +"\" and team=\"" + str(acquisition.team) + "\" and comment=\"" + str(acquisition.comment) + "\";"
		#print query
		for data in self.connector.mcfQuery(query):
			newID = data[0]
		
		#Insert the new relation in the local ontology
		acquisition.ac_id = newID
		self.all_acquisitions[newID] = acquisition
		#return the relation_id
		
		return acquisition


	def addInstance( self, newInstance):
		"""Add an instance attribute in the database"""
		query="INSERT INTO Instances ( instances_id, acquisition_id, AE_id) VALUES (NULL,'"+ str(newInstance.ac_id) +"','"+ str(newInstance.ae_id) +"');"
		#print query
		self.connector.mcfQueryExec(query)
		
		# Get the new ID
		query="SELECT instances_id FROM Instances WHERE acquisition_id=\""+ str(newInstance.ac_id) +"\" AND AE_id=\""+ str(newInstance.ae_id) + "\";"
		#print query
		print "pour cette instance " + newInstance.getName() + ", acqId: "+ str(newInstance.ac_id) + " aeId: " + str(newInstance.ae_id)
		for data in self.connector.mcfQuery(query):
			newID = int(data[0])
			print "j'ai bien trouve son id: " + str(newID)
		
		#Insert the new relation in the local ontology
		newInstance.instances_id = newID
		self.all_instances_attributes[newID] = newInstance
		print "Aufinal, l'id est: " + str(newID)
		#return the newInstance
		return newInstance


	def addInstanceAttribute( self, newInstanceAttribute):
		"""Add an instance attribute in the database"""
		query="INSERT INTO Instance_Attributes ( instances_attributes_id, instances_id, instance_attribute_type_id, value) VALUES (NULL,'"+ str(newInstanceAttribute.instance_id) +"','"+ str(newInstanceAttribute.attribute_type_id) +"','"+ str(newInstanceAttribute.value) +"');"
		#print query
		self.connector.mcfQueryExec(query)
		
		# Get the new ID
		query="SELECT instances_attributes_id FROM Instance_Attributes where instances_id=\""+ str(newInstanceAttribute.instance_id) +"\" and instance_attribute_type_id=\""+ str(newInstanceAttribute.attribute_type_id) +"\" and value=\""+ str(newInstanceAttribute.value) + "\";"
		#print query
		for data in self.connector.mcfQuery(query):
			newID = int(data[0])
		
		#Insert the new relation in the local ontology
		newInstanceAttribute.instance_attribute_id = newID
		self.all_instances_attributes[newID] = newInstanceAttribute
		#return the relation_id
		return newInstanceAttribute


	def get_all_acquisitions( self):
		"""Load all the acquisitions from myCorporisFabrica"""
		query="SELECT * FROM Acquisitions"
		for data in self.connector.mcfQuery(query):
			self.all_acquisitions[int(data[0])] = Acquisition( libmyCF=self.libmyCF, ac_id=int(data[0]), ae_id=int(data[1]), type=data[2], date=data[3], team=data[4], comment=data[5])


	def get_all_instances( self):
		"""Load all the instances from myCorporisFabrica"""
		query="SELECT * FROM Instances"
		for data in self.connector.mcfQuery(query):
			self.all_instances[int(data[0])] = Instance( libmyCF=self.libmyCF, instance_id=int(data[0]), ac_id=int(data[1]), ae_id=int(data[2]))


	def get_all_instances_attributes( self):
		"""Load all the instances attributes from myCorporisFabrica"""
		query="SELECT * FROM Instance_Attributes"
		for data in self.connector.mcfQuery(query):
			self.all_instances_attributes[int(data[0])] = Instance_Attribute( self.libmyCF, instance_attribute_id=int(data[0]), instance_id=int(data[1]), attribute_type_id=int(data[2]), value=data[3])


	def getAcquisitionsOfTheOrgan( self, organName):
		"""Returns a dictionary of acquisitions corresponding to the organ 'organName'"""
		result = {}
		for key, acquisition in self.all_acquisitions.iteritems() :
			if( acquisition.getName() == organName) :
				result[key] = acquisition
		return result


	def getInsertionsPartOf( self, instance):
		"""Retrieve the instances part of the instance 'instance' from DataBase"""
		relType = self.libmyCF.ontology.getRelationTypeNamed( "part_of")
		relID = relType.relation_type_id
		
		result = []
		aesPartOf = instance.getAE().get_l_child( relID)
		
		acquisition = self.all_acquisitions[ instance.ac_id ]
		instances = acquisition.getInstances()
		
		for inst in instances:
			for ae in aesPartOf:
				if( inst.getAE().getId() == ae.getId()):
					result.append( inst)
		
		return result
